<template>
	<div class="components-container">
    <div class="header-form">
        <div class="layui-inline">
            <label class="layui-form-label" >用户名称</label>
              <div class="layui-input-inline">
            <el-input  v-model="selectData.NameLike" placeholder="请输入用户名称"></el-input>
              </div>
        </div>
        <el-button type="primary" @click="btnSearch" >搜索</el-button>
        <el-button  @click="btnRepeat">重置</el-button>
    </div>
    <div>
        <ul class="toolList">
            <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
                <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
            </li>
        </ul>
    </div>
    <!--数据列表-->
    <dataTable :headList="headList" :dataList="list" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
    <usr ref="users" @refreshData="getData"></usr>
    <staEndUse ref="useMain" @refreshData="getData"></staEndUse>
	</div>

</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>
<script>
import apiBase from "@/api/apiBase.js";
import dataTable from "@/components/Table/DataTable";
import { manage } from "@/mixins/managePage.js";
import staEndUse from "../basic/staEndUse";
import { ORDER } from "@/const/orderConst.js";
import usr from "./userMain";

const ADD = "新增";
const MODIFY = "修改";
const DELETE = "删除";
const INFO = "详情";
const RESET = "密码重置";

export default {
  name: "userManage",
  mixins: [manage],
  data() {
    return {
      list: [],
      selectData: {}
    };
  },
  methods: {
    //功能操作
    operate(order) {
      this.order = order;
      switch (this.order) {
        case ADD:
          this.$refs.users.open(this.order);
          break;
        case MODIFY:
        case DELETE:
        case INFO:
          this.$refs.users.open(this.order, this.currentRow.USRID);
          break;
        case RESET:
          this.pasReset(this.currentRow.USRID);
          break;
        case ORDER.START:
        case ORDER.END:
          this.$refs.useMain.open(this.order, this.currentRow.USRID);
          break;
        default:
      }
    },

    //获取列项
    getColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetUserColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },

    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      var data = {
        PageIndex: this.pageOrder.currentPage,
        PageSize: this.pageOrder.pageSize,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      apiBase.DoService(data, apiBase.getApiAdress().User.Query, res => {
        this.list = res.data.content.OutData;

        this.pageOrder.totalNum = res.data.content.RecordCount;
      });
    },
    getRoles() {
      var data = {};
      apiBase.DoService(data, apiBase.getApiAdress().User.QueryRole, res => {
        let userRoleData = res.data.content,
          legendsUsersData = userRoleData.map(item => ({
            label: item.ROLNAME,
            value: item.ROLID
          }));
        this.useRoleOptions = legendsUsersData;
      });
    },
    //部门查询
    getUserDept() {
      var data = {};
      apiBase.DoService(data, apiBase.getApiAdress().User.QueryDept, res => {
        let userDptData = res.data.content,
          legendsDptsData = userDptData.map(item => ({
            label: item.DPTNAME,
            value: item.DPTID
          }));
        this.userDeptNos = legendsDptsData;
        //  console.log('userDeptNos',this.userDeptNos)
      });
    },
    btnSearch() {
      this.getData();
    },
    btnShow(btn) {
      let status = "";
      if (this.currentRow) {
        //console.log(this.currentRow);
        status = this.currentRow.USRSTATUS;
      }
      switch (btn) {
        case ADD:
          return true;
          break;
        case INFO:
        case MODIFY:
        case DELETE:
        case RESET:
          return status !== "";
          break;
        case ORDER.END:
          return status === "A";
          break;
        case ORDER.START:
          return status === "B";
        default:
      }
    },
    btnRepeat() {
      this.selectData = {};
      this.getData();
    },
    pasReset(id) {
      this.$confirm("此操作将会重置该密码, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(callback => {
        apiBase.DoService(
          {
            USRID: id
          },
          apiBase.getApiAdress().User.ResetPassword,
          res => {
            this.$message({
              type: "success",
              message: "密码重置成功!"
            });
          }
        );
      });
    }
  },
  mounted() {},
  components: {
    usr,
    dataTable,
    staEndUse
  },
  created() {
    this.getUserDept();
    this.getRoles();
    this.getColumns();
    this.getData();
  }
};
</script>